package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.service.ImportService;
import com.deyuanyun.pic.ctrl.util.AutoImport;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.service.pipe.PipeLineService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/11/11）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class PipeLineValidata implements ValidateInterface {

    @Override
    public <T, E,C> boolean apply(T t, E e,C c) {
        if(ObjectUtil.isNotNullEmpty(t)){

            PipeLineService service= SpringAppliction.getBean(PipeLineService.class);
            ImportService importService= SpringAppliction.getBean(ImportService.class);
            PipeLine pipeLine= service.getLineByName(((String)t).trim());//PipeDictUtils.getLineIdByName(((String) t).trim());
            if (pipeLine == null || "1".equals(pipeLine.getState())){
                return false;
            }
            Field f=(Field) e;

            String lineId = importService.queryInspLineId(AutoImport.getExInspTaskId());
            if(pipeLine == null || !pipeLine.getId().equals(lineId))
            {
                return false;
            }
            try {
                BeanUtils.setProperty(c,f.getName(),pipeLine.getId());
            } catch (Exception e1) {
                e1.printStackTrace();
                return false;
            }
            return true;
        }
        return true;
    }

    @Override
    public String getName() {
        return null;
    }

}
